import {
  appointmentStudyRoom,
  auditReservation,
  evaluateStudyRoom,
  getAppointment,
  getAppointmentByStudyRoom,
  getStudyRoom,
  likeSearchStudyRoom,
  releaseStudyRoom,
  getAppointmentTimeQuantum,
  getRevenue,
  updateReservePerson
} from "@/service/studyRoom/studyRoom"
import { defineStore } from "pinia"

interface IStudyRoomState {
  studyRoom: IParams
  roomAppointment: IParams
  roomAppointmentSuccess: IParams
  appointmentByStudyRoom: IParams
  appointmentTimeQuantum: IParamsData[]
  revenue: IParamsData2[]
}
interface IParams {
  lists: any[]
  total: number
}
interface IParamsData {
  name: string
  value: number
}
interface IParamsData2 {
  reservation_date: string
  daily_revenue: string
}
const useStudyRoomStore = defineStore("studyRoom", {
  state: (): IStudyRoomState => ({
    studyRoom: {
      lists: [],
      total: 0
    },
    roomAppointment: {
      lists: [],
      total: 0
    },
    roomAppointmentSuccess: {
      lists: [],
      total: 0
    },
    appointmentByStudyRoom: {
      lists: [],
      total: 0
    },
    appointmentTimeQuantum: [
      {
        value: 40,
        name: "08:00-09:00"
      },
      {
        value: 38,
        name: "09:00-10:00"
      },
      { value: 32, name: "10:00-11:00" },
      { value: 30, name: "11:00-12:00" },
      { value: 28, name: "12:00-13:00" },
      { value: 26, name: "13:00-14:00" },
      { value: 22, name: "14:00-15:00" },
      { value: 18, name: "15:00-16:00" }
    ],
    revenue: [
      {
        reservation_date: "",
        daily_revenue: ""
      }
    ]
  }),
  actions: {
    async releaseStudyRoom(data) {
      const result = await releaseStudyRoom(data)
      if (result.data.code == 200) {
        return "ok"
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async getStudyRoom(longitude, latitude, limit = 3, offset = 0) {
      const result = await getStudyRoom(longitude, latitude, limit, offset)
      // console.log("erer", result)
      if (result.data.code == 200) {
        this.studyRoom.lists = result.data.data.lists
        this.studyRoom.total = result.data.data.total
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async likeSearchStudyRoom(keyWord, limit = 3, offset = 0) {
      const result = await likeSearchStudyRoom(keyWord, limit, offset)
      // console.log(result)

      if (result.data.code == 200) {
        console.log("查询自习室数据", result.data.data)

        this.studyRoom.lists = result.data.data.lists
        this.studyRoom.total = result.data.data.total
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async appointmentStudyRoom(data) {
      const result = await appointmentStudyRoom(data)
      console.log(result)

      if (result.data.code == 200) {
        return "ok"
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async getAppointment(limit = 3, offset = 0) {
      const result = await getAppointment(limit, offset)
      console.log("erer123", result)
      if (result.data.code == 200) {
        this.roomAppointment.lists = result.data.data.lists
        this.roomAppointment.total = result.data.data.total
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async getAppointmentSuccess(limit = 3, offset = 0) {
      const result = await getAppointment(limit, offset, true)
      console.log("erer123", result)
      if (result.data.code == 200) {
        this.roomAppointmentSuccess.lists = result.data.data.lists
        this.roomAppointmentSuccess.total = result.data.data.total
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async evaluateStudyRoom(data) {
      const result = await evaluateStudyRoom(data)
      if (result.data.code == 200) {
        return "ok"
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async getAppointmentByStudyRoom(limit = 3, offset = 0) {
      const result = await getAppointmentByStudyRoom(limit, offset)
      console.log("erer123", result)
      if (result.data.code == 200) {
        this.appointmentByStudyRoom.lists = result.data.data.lists
        this.appointmentByStudyRoom.total = result.data.data.total
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async auditReservation(reservationId, audit_id) {
      const result = await auditReservation(reservationId, audit_id)
      if (result.data.code == 200) {
        return "ok"
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async updateReservePerson(room_id, state) {
      const result = await updateReservePerson(room_id, state)
      if (result.data.code == 200) {
        return "ok"
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async getAppointmentTimeQuantum() {
      const result = await getAppointmentTimeQuantum()
      if (result.data.code == 200) {
        this.appointmentTimeQuantum = result.data.data
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async getRevenue() {
      const result = await getRevenue()
      if (result.data.code == 200) {
        this.revenue = result.data.data
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    }
  },
  getters: {
    dateRevenue(state) {
      const revenueData = {
        xAxisData: [],
        data: []
      }
      state.revenue.forEach((item) => {
        revenueData.xAxisData.push(item.reservation_date as never)
        revenueData.data.push(item.daily_revenue as never)
      })
      return revenueData
    }
  }
})

export default useStudyRoomStore
